package iamutkarshtiwari.github.io.ananas.editimage.adapter;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.fragment.paint.BrushConfigDialog;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.List;

/**
 *  颜色选择适配器
 */
public class ColorPickerAdapter extends BaseItemProvider {

    private List<Integer> colorPickerColors;
    private Context mContext;
    private BrushConfigDialog dialog;

    /**
     * 构造
     *
     * @param context Context对象
     * @param dialog  BrushConfigDialog对象
     * @param colors  颜色列表
     */
    public ColorPickerAdapter(Context context, BrushConfigDialog dialog, List<Integer> colors) {
        super();
        this.mContext = context;
        this.dialog = dialog;
        this.colorPickerColors = colors;
    }

    @Override
    public int getCount() {
        return colorPickerColors.size();
    }

    @Override
    public Object getItem(int position) {
        return colorPickerColors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_color_picker_item_list, null, false);
        } else {
            cpt = component;
        }

        Component showView = cpt.findComponentById(ResourceTable.Id_color_picker_view);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(colorPickerColors.get(position)));
        showView.setBackground(shapeElement);

        showView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dialog.changColor(position);
            }
        });

        return cpt;
    }
}
